package com.tarena.lab;
import java.sql.*;
public class Lab2
{
	public static void main(String[] args){
	   if(args.length!=2){
	     System.out.println("用法:java Lab2 表名 create/drop");
		 return;
	   }
	   String tableName = args[0];
	   String type = args[1];
	   String driverName = 
		   "oracle.jdbc.driver.OracleDriver";
	   String url = 
		  "jdbc:oracle:thin:@192.168.0.20:1521"+
		  ":tarena";
	   String userName="openlab";
	   String pwd = "open123";
	   Connection con = null;
	   Statement stmt = null;
	   try{
	     //1.加载驱动
		 Class.forName(driverName);
		 //2.连数据库
		 con = DriverManager.getConnection(
			       url,userName,pwd);
		 //3.获取数据库操作对象(SQL)
         stmt = con.createStatement();

		 String sql = "";
		 if("create".equals(type)){
		    sql = "create table "+tableName+
			   "(ID number(12) primary key,"+
			   "NAME varchar(10) not null)";
         }else if("drop".equals(type)){
		    sql = "drop table "+tableName;
		 }else{
		    System.out.println("不支持的命令:"+type);
			return;
		 }
		 stmt.executeUpdate(sql);
         System.out.println(type+"操作成功");
	   }catch(Exception e){
		  //e.printStackTrace();
		  System.out.println(type+"操作失败");
	   }finally{
		  try{
		    stmt.close();
		  }catch(Exception ex){
		    ex.printStackTrace();
		  }
	      try{
		    con.close();
		  }catch(Exception ex){
		    ex.printStackTrace();
		  }
		  
	   }
	}
}